//
//  MsgFriendCell.m
//  Safflower
//
//  Created by 袁亮 on 2016/12/19.
//  Copyright © 2016年 Migic_Z. All rights reserved.
//

#import "MsgFriendCell.h"
#import "MsgFriendListCell.h"

@interface MsgFriendCell()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *list_table_view;

@end

@implementation MsgFriendCell

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:BecomeFriendNotification object:nil];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = CLEARCOLOR;
        
        [self makeTableView];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshingListData) name:BecomeFriendNotification object:nil];
        
    }
    return self;
}

- (void)makeTableView
{
    _list_table_view = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
    _list_table_view.dataSource = self;
    _list_table_view.delegate = self;
    _list_table_view.separatorStyle = UITableViewCellSeparatorStyleNone;
    _list_table_view.backgroundColor = CLEARCOLOR;
    [self addSubview:_list_table_view];
    [_list_table_view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.left.right.mas_equalTo(0);
    }];
    _list_table_view.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _list_table_view.tableFooterView = nil;
        if ([self.delegate respondsToSelector:@selector(friendHeaderWithRefreshingData)]) {
            [self.delegate friendHeaderWithRefreshingData];
        }
    }];

    [self refreshingListData];
}

- (void)refreshingListData
{
    [_list_table_view.mj_header beginRefreshing];
}

- (void)setData:(NSArray *)data
{
    _data = data;
    if (_data.count == 0) {
        _list_table_view.tableFooterView = [[YJDataStateView sharedStateView] showStateWithTitle:@"暂无好友消息"];
    }

    [_list_table_view reloadData];
    [_list_table_view.mj_header endRefreshing];
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _data.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MsgFriendListCell *listCell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([MsgFriendListCell class])];
    if (!listCell) {
        listCell = [[MsgFriendListCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass([MsgFriendListCell class])];
    }
    
    __weak __typeof(self) weakSelf = self;
    listCell.agreeBlock = ^(){
        [weakSelf didSelectAgreeAtIndex:indexPath.row];
    };
    
    
    [listCell loadData:(FriendModel *)[_data objectAtIndex:indexPath.row] index:indexPath.row];
    
    return listCell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 58;
}

//- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
//{
//    if (_data.count == 0) {
//        return 20;
//    }
//    return 0;
//}
//
//- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
//{
//    if (_data.count == 0) {
//        return [[YJDataStateView sharedStateView] showStateWithTitle:@"暂无好友"];
//    }
//    return nil;
//}

- (void)didSelectAgreeAtIndex:(NSInteger)index
{
    FriendModel *model = [_data objectAtIndex:index];
    if ([self.delegate respondsToSelector:@selector(didSelectAgreeIndexFriend:)]) {
        [self.delegate didSelectAgreeIndexFriend:model];
    }
}


@end
